home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993 October: Windmill on DISC / ADC Developer CD (1993-10) (''Windmill On DISC'')_iso / Dev.CD Oct 93.iso / System Software / U.S. System Software / System 7 Pro™ Beta 11 / Development Tools / Sample Code / Standard Mail / CollaboDraw (w⁄DigiSign) / windutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  9.3 KB  |  374 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample AOCE Application Framework
  4.  *
  5.  * ©1991-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * windutils.c -- window drawing/scrolling utility functions
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        04/21/93        1.0b2        update to b2
  14.  * SJF        03/01/93        1.0b1        added digital signatures
  15.  * SJF        02/09/93        1.0b1        update to b1
  16.  * SJF        10/13/92        1.0d4        update to a11
  17.  * SJF        09/09/92        1.0d3        update to a9
  18.  * SJF        05/07/92        1.0d2        update to a6
  19.  * SJF        11/06/91        1.0d1        initial coding
  20.  *
  21.  */
  22.  
  23. #ifndef __OCESTANDARDMAIL__
  24. #include <OCEStandardMail.h>
  25. #endif
  26.  
  27. #include "const.h"
  28. #include "strconst.h"
  29. #include "mytypes.h"
  30. #include "globals.h"
  31. #include "utils.h"
  32. #include "windowstuff.h"
  33. #include "myaocetypes.h"
  34. #include "myaoce.h"
  35. #include "draw.window.h"
  36.  
  37. #include "windutils.h"
  38.  
  39.  
  40.  
  41. void MyDrawGrowIcon(WindowPtr window)
  42. {
  43.     GrafPtr savePort;
  44.     RgnHandle saveClip;
  45.     Rect gbRect;
  46.     
  47.     gbRect = window->portRect;
  48.     gbRect.top = gbRect.bottom - kGrowBoxWidth;
  49.     gbRect.left = gbRect.right - kGrowBoxWidth;
  50.     GetPort(&savePort);
  51.     SetPort(window);
  52.     
  53.     saveClip = NewRgn();
  54.     GetClip(saveClip);
  55.     ClipRect(&gbRect);
  56.     
  57.     DrawGrowIcon(window);
  58.     
  59.     SetClip(saveClip);
  60.     DisposeRgn(saveClip);
  61.     
  62.     SetPort(savePort);
  63. }
  64.  
  65.  
  66. void ClipToDrawing(WindowPtr window,WInfoPtr infoPtr)
  67. {
  68.     Rect newClipRect;
  69.     
  70.     newClipRect = window->portRect;
  71.     newClipRect.top += infoPtr->topIndent;
  72.     newClipRect.left += infoPtr->leftIndent;
  73.     ClipRect(&newClipRect);
  74. }
  75.  
  76.  
  77. RgnHandle GetScrollersRgn(Rect *windowRect)
  78. {
  79.     RgnHandle windowRgn;
  80.     Rect wRect;
  81.     
  82.     windowRgn = NewRgn();
  83.  
  84.     OpenRgn();
  85.     SetRect(&wRect,windowRect->left,windowRect->bottom-kScrollBarWidth,
  86.                 windowRect->right-kScrollBarWidth,windowRect->bottom);
  87.     FrameRect(&wRect);
  88.     SetRect(&wRect,windowRect->right-kScrollBarWidth,windowRect->top,
  89.                 windowRect->right,windowRect->bottom);
  90.     FrameRect(&wRect);
  91.     CloseRgn(windowRgn);
  92.     
  93.     return windowRgn;
  94. }
  95.  
  96.  
  97. void MakeScrollBars(WindowPtr window,WInfoPtr infoPtr)
  98. {
  99.     GrafPtr savePort;
  100.     Rect cntrlRect;
  101.     
  102.     infoPtr->otherData[kViewOffset] = 0;
  103.     
  104.     GetPort(&savePort);
  105.     SetPort(window);
  106.  
  107.     cntrlRect.left += infoPtr->leftIndent;
  108.     cntrlRect = window->portRect;
  109.     InsetRect(&cntrlRect,-1,-1);
  110.     cntrlRect.top = cntrlRect.bottom - kScrollBarWidth;
  111.     cntrlRect.right = cntrlRect.right - kGrowBoxWidth;
  112.     HSCROLL = NewControl(window,&cntrlRect,"\p",false,0,0,0,scrollBarProc,0);
  113.     ValidRect(&cntrlRect);
  114.  
  115.     cntrlRect = window->portRect;
  116.     InsetRect(&cntrlRect,-1,-1);
  117.     cntrlRect.left = cntrlRect.right - kScrollBarWidth;
  118.     cntrlRect.bottom = cntrlRect.bottom - kGrowBoxWidth;
  119.     cntrlRect.top += infoPtr->topIndent;
  120.     VSCROLL = NewControl(window,&cntrlRect,"\p",false,0,0,0,scrollBarProc,0);
  121.     ValidRect(&cntrlRect);
  122.  
  123.     ReCalcScrollBars(window,infoPtr);
  124.     ShowControl((ControlHandle)HSCROLL);
  125.     ShowControl((ControlHandle)VSCROLL);
  126.     
  127.     SetPort(savePort);
  128. }
  129.  
  130.  
  131. void MoveScrollBars(WindowPtr window)
  132. {
  133.     WInfoPtr infoPtr;
  134.     char hState;
  135.     Rect cntrlRect;
  136.     GrafPtr savePort;
  137.     
  138.     GetPort(&savePort);
  139.     SetPort(window);
  140.     
  141.     infoPtr = BeginWindowAccess(window,&hState);
  142.  
  143.     HideControl((ControlHandle)VSCROLL);
  144.     HideControl((ControlHandle)HSCROLL);
  145.  
  146.     if (infoPtr->topIndent<=(window->portRect.bottom-kGrowBoxWidth) &&
  147.         infoPtr->leftIndent<=(window->portRect.right-kGrowBoxWidth)) {
  148.         
  149.         cntrlRect = window->portRect;
  150.         InsetRect(&cntrlRect,-1,-1);
  151.         cntrlRect.top = cntrlRect.bottom - kScrollBarWidth;
  152.         cntrlRect.right = cntrlRect.right - kGrowBoxWidth;
  153.         cntrlRect.left += infoPtr->leftIndent;
  154.         MoveControl(HSCROLL,cntrlRect.left,cntrlRect.top);
  155.         SizeControl(HSCROLL,cntrlRect.right-cntrlRect.left,cntrlRect.bottom-cntrlRect.top);
  156.         ValidRect(&cntrlRect);
  157.     
  158.         cntrlRect = window->portRect;
  159.         InsetRect(&cntrlRect,-1,-1);
  160.         cntrlRect.left = cntrlRect.right - kScrollBarWidth;
  161.         cntrlRect.bottom = cntrlRect.bottom - kGrowBoxWidth;
  162.         cntrlRect.top += infoPtr->topIndent;
  163.         MoveControl(VSCROLL,cntrlRect.left,cntrlRect.top);
  164.         SizeControl(VSCROLL,cntrlRect.right-cntrlRect.left,cntrlRect.bottom-cntrlRect.top);
  165.         ValidRect(&cntrlRect);
  166.     
  167.         ReCalcScrollBars(window,infoPtr);
  168.         
  169.         ShowControl(HSCROLL);
  170.         ShowControl(VSCROLL);
  171.  
  172.     }
  173.     
  174.     EndWindowAccess(window,hState);
  175.     SetPort(savePort);
  176. }
  177.  
  178.  
  179. void ReCalcScrollBars(WindowPtr window,WInfoPtr infoPtr)
  180. {
  181.     Rect viewBounds,pageRect;
  182.     short vMax,hMax;
  183.     Point *scrollPos;
  184.     
  185.     GetViewBounds(window,infoPtr,&viewBounds);
  186.     pageRect = (**(infoPtr->printRecord)).prInfo.rPage;
  187.     scrollPos = (Point *)&infoPtr->otherData[kViewOffset];
  188.     
  189.     hMax = (pageRect.right-pageRect.left) - (viewBounds.right-viewBounds.left);
  190.     vMax = (pageRect.bottom-pageRect.top) - (viewBounds.bottom-viewBounds.top);
  191.     
  192.     if (hMax<0)
  193.         hMax=0;
  194.     if (vMax<0)
  195.         vMax=0;
  196.     
  197.     if ( (scrollPos->h+(viewBounds.right-viewBounds.left)) > (pageRect.right-pageRect.left) )
  198.         scrollPos->h = (pageRect.right-pageRect.left) - (viewBounds.right-viewBounds.left);
  199.     if ( (scrollPos->v+(viewBounds.bottom-viewBounds.top)) > (pageRect.bottom-pageRect.top) )
  200.         scrollPos->v = (pageRect.bottom-pageRect.top) - (viewBounds.bottom-viewBounds.top);
  201.         
  202.     SetCtlMax((ControlHandle)HSCROLL,hMax);
  203.     SetCtlMax((ControlHandle)VSCROLL,vMax);
  204.     SetCtlValue((ControlHandle)HSCROLL,scrollPos->h);
  205.     SetCtlValue((ControlHandle)VSCROLL,scrollPos->v);
  206.     SetPort(window);
  207. }
  208.  
  209.  
  210. pascal void ScrollActionProc(ControlHandle theControl,short part)
  211. {
  212.     short ctlValue,delta,ctlMin,ctlMax,pageDistance,deltaH,deltaV;    
  213.     Rect viewRect;
  214.     WindowPtr window;
  215.     WInfoPtr infoPtr;
  216.     char hState;
  217.     
  218.     if (part==0)
  219.         return;
  220.         
  221.     window = (**theControl).contrlOwner;
  222.     if (!IsAppWindow(window))
  223.         return;
  224.     
  225.     infoPtr = BeginWindowAccess(window,&hState);
  226.     
  227.     GetViewBounds(window,infoPtr,&viewRect);
  228.     pageDistance = viewRect.bottom-viewRect.top;
  229.     
  230.     ctlValue = GetCtlValue(theControl);
  231.     ctlMin = GetCtlMin(theControl);
  232.     ctlMax = GetCtlMax(theControl);
  233.     
  234.     switch (part) {
  235.         case inUpButton:
  236.             delta = -kScrollDistance;
  237.             break;
  238.         case inDownButton:
  239.             delta = kScrollDistance;
  240.             break;
  241.         case inPageUp:
  242.             delta = -pageDistance;
  243.             break;
  244.         case inPageDown:
  245.             delta = pageDistance;
  246.             break;
  247.     }
  248.         
  249.     if ( (ctlValue+delta)>ctlMax )
  250.         delta = ctlMax-ctlValue;
  251.     else if ( (ctlValue+delta)<ctlMin )
  252.         delta = ctlMin-ctlValue;
  253.         
  254.     SetCtlValue(theControl,ctlValue+delta);
  255.     
  256.     deltaH = deltaV = 0;
  257.     if (theControl==(ControlHandle)HSCROLL)
  258.         deltaH = delta;
  259.     else if (theControl==(ControlHandle)VSCROLL)
  260.         deltaV = delta;
  261.     
  262.     MoveGraphics(window,infoPtr,deltaH,deltaV);
  263.  
  264.     EndWindowAccess(window,hState);
  265. }
  266.  
  267.  
  268. void MoveGraphics(WindowPtr window,WInfoPtr infoPtr,short deltaH,short deltaV)
  269. {
  270.     GrafPtr savePort;
  271.     Rect drawingArea;
  272.     RgnHandle updateRgn,saveClip;
  273.     Point *scrollPos,drawOffset;
  274.     
  275.     GetDrawingAreaBounds(window,infoPtr,&drawingArea);
  276.     
  277.     // store new scroll position into window data
  278.     
  279.     scrollPos = (Point *)&infoPtr->otherData[kViewOffset];
  280.     scrollPos->h += deltaH;
  281.     scrollPos->v += deltaV;
  282.  
  283.     drawOffset = *scrollPos;
  284.     drawOffset.h = infoPtr->leftIndent-drawOffset.h;
  285.     drawOffset.v = infoPtr->topIndent-drawOffset.v;
  286.     
  287.     // do drawing of scrolling
  288.     
  289.     GetPort(&savePort);
  290.     SetPort(window);
  291.     updateRgn = NewRgn();
  292.     saveClip = NewRgn();
  293.     ScrollRect(&drawingArea,-deltaH,-deltaV,updateRgn);
  294.     GetClip(saveClip);
  295.     SetClip(updateRgn);
  296.     DrawAllShapes(infoPtr,drawOffset);
  297.     SetClip(saveClip);
  298.     DisposeRgn(updateRgn);
  299.     DisposeRgn(saveClip);
  300.     SetPort(savePort);
  301. }
  302.  
  303.  
  304. Boolean CheckPageSize(WindowPtr window,WInfoPtr infoPtr)
  305. {
  306.     Rect oldSize;
  307.     short newWidth,newHeight,oldWidth,oldHeight;
  308.     GrafPtr savePort;
  309.     Boolean changed;
  310.     
  311.     ClipPageSize(&window->portRect,infoPtr,&newWidth,&newHeight);
  312.     oldWidth = window->portRect.right-window->portRect.left;
  313.     oldHeight = window->portRect.bottom-window->portRect.top;
  314.     
  315.     // force a new window size on the user since they changed page setup on us
  316.     
  317.     if (newHeight!=oldHeight || newWidth!=oldWidth) {
  318.         GetPort(&savePort);
  319.         SetPort(window);
  320.         oldSize = window->portRect;
  321.         SizeWindow(window,newWidth,newHeight,true);
  322.         InvalRect(&window->portRect);
  323.         SendWindowMessage(window,kResizeMessage,&oldSize);
  324.         SetPort(savePort);
  325.         changed = true;
  326.     }    
  327.     else
  328.         changed = false;
  329.     
  330.     return changed;
  331. }
  332.  
  333.  
  334. void ClipPageSize(Rect *wRect,WInfoPtr infoPtr,short *maxWidth,short *maxHeight)
  335. {
  336.     Rect printRect,drawAreaRect;
  337.     
  338.     printRect = (**(infoPtr->printRecord)).prInfo.rPage;
  339.  
  340.     drawAreaRect.top = infoPtr->topIndent;
  341.     drawAreaRect.left = infoPtr->leftIndent;
  342.     drawAreaRect.bottom = wRect->bottom-wRect->top-kScrollBarWidth;
  343.     drawAreaRect.right = wRect->right-wRect->left-kScrollBarWidth;
  344.  
  345.     *maxWidth = wRect->right-wRect->left;
  346.     *maxHeight = wRect->bottom-wRect->top;
  347.     if ( (drawAreaRect.right-drawAreaRect.left) > (printRect.right-printRect.left) )
  348.         *maxWidth = (printRect.right-printRect.left)+infoPtr->leftIndent+kScrollBarWidth;
  349.     if ( (drawAreaRect.bottom-drawAreaRect.top) > (printRect.bottom-printRect.top) )
  350.         *maxHeight = (printRect.bottom-printRect.top)+infoPtr->topIndent+kScrollBarWidth;
  351. }
  352.  
  353.  
  354. void GetViewBounds(WindowPtr window,WInfoPtr infoPtr,Rect *bounds)
  355. {
  356.     Point viewOffset;
  357.     
  358.     viewOffset = *(Point *)&infoPtr->otherData[kViewOffset];
  359.     GetDrawingAreaBounds(window,infoPtr,bounds);
  360.     OffsetRect(bounds,-infoPtr->topIndent,-infoPtr->leftIndent);
  361.     OffsetRect(bounds,viewOffset.h,viewOffset.v);
  362. }
  363.  
  364.  
  365. void GetDrawingAreaBounds(WindowPtr window,WInfoPtr infoPtr,Rect *bounds)
  366. {
  367.     bounds->top = infoPtr->topIndent;
  368.     bounds->left = infoPtr->leftIndent;
  369.     bounds->bottom = window->portRect.bottom-window->portRect.top-kScrollBarWidth;
  370.     bounds->right = window->portRect.right-window->portRect.left-kScrollBarWidth;
  371. }
  372.  
  373.  
  374.